/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.serviportales.serviquality.managers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.serviportales.serviquality.dao.TipoNodoDAO;
import com.serviportales.serviquality.exception.ServiqualityException;
import com.serviportales.serviquality.model.TipoNodo;

@Service
@Transactional(rollbackFor = { ServiqualityException.class })
public class MgrTiposNodos {


	@Autowired
	private TipoNodoDAO tipoNodoDAO;
	@Autowired
	private MessageSource messageSource;

	public void grabarTipo(TipoNodo tipoNodo) throws ServiqualityException {
		try {
			TipoNodo tipoNodoFilter = tipoNodoDAO.selectByID(tipoNodo.getCodigo());
			if (tipoNodoFilter != null) {
				tipoNodoFilter.setDescripcion(tipoNodo.getDescripcion());
				tipoNodoFilter.setEstadoRegistro(tipoNodo.getEstadoRegistro());
				tipoNodoFilter.setNivel(tipoNodo.getNivel());
				tipoNodoFilter.setNombre(tipoNodo.getNombre());
				tipoNodoDAO.update(tipoNodoFilter);
			} else {
				tipoNodoDAO.insert(tipoNodo);
			}
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.GRABAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}

	public List<TipoNodo> buscarTipo(TipoNodo tipoNodo) throws ServiqualityException {
		try {
			return tipoNodoDAO.select(tipoNodo);
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.BUSCAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}

	public List<TipoNodo> buscarTipo(TipoNodo tipoNodo, int maxResult) throws ServiqualityException {
		try {
			return tipoNodoDAO.select(tipoNodo, 0, maxResult);
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.BUSCAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}
}
